package mobi.pooper;

import static mobi.pooper.Constants.*;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.RotateTo;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Scaling;

import static mobi.pooper.Constants.*;

public class ResultScreen implements Screen {

	protected static final String TAG = ResultScreen.class.getSimpleName();

	private Pooper pooper;
	private Stage stage;
	
	private Texture texture;
	private Sound sound;
	
	private Sprite bgSprite;
		
	private ClickListener clkListener = new ClickListener(){
		@Override
		public void click(Actor arg0, float arg1, float arg2) {
			sound.stop();
			pooper.resultHaveSeen();
		}
	};

	public ResultScreen(Pooper pooper, int result) {
		this.pooper = pooper;

		stage = new Stage(NATIVE_WIDTH, NATIVE_HEIGHT, false);
		
		setBackgroundImage();
		
		Image img;
		if(result == Pooper.YES){
			texture = new Texture(Gdx.files.internal("data/images/ok.jpg"));
			img = new Image(texture, Scaling.fit, Align.CENTER);
			img.action(RotateTo.$(1080, 1.5F));
			sound = Assets.applause1;
		}else{
			texture = new Texture(Gdx.files.internal("data/images/no.jpg"));
			img = new Image(texture, Scaling.fit, Align.CENTER);
			img.action(RotateTo.$(-1080, 1.5F));
			sound = Assets.negative;
		}

		img.x = NATIVE_WIDTH / 2 - PICTURE_WIDTH;
		img.y = NATIVE_HEIGHT / 2 - PICTURE_HEIGHT;
		img.originX = PICTURE_WIDTH;
		img.originY = PICTURE_HEIGHT;
		img.width = PICTURE_WIDTH * 2;
		img.height = PICTURE_HEIGHT * 2;
		stage.addActor(img);
		
		sound.play();
		
		Gdx.input.setInputProcessor(stage);
		img.setClickListener(clkListener);
		
	}

	private void setBackgroundImage() {
        bgSprite = new Sprite(Assets.background1, 0, 0, NATIVE_WIDTH, NATIVE_HEIGHT);
        bgSprite.setBounds(0, 0, Assets.background1.getWidth(), Assets.background1.getHeight());    
	}

	@Override
	public void dispose() {
		texture.dispose();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		SpriteBatch sb = stage.getSpriteBatch();
		
		sb.begin();
		bgSprite.draw(sb);
		sb.end();
		
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
}
